﻿using System.Linq;

namespace YunSpace.Application.Messaging
{
    /// <summary>
    /// 消息发送者 Facade
    /// </summary>
    public class MessageSenderFacade
    {
        private readonly MessageSenderType[] _senderTypes;

        public MessageSenderFacade(MessageSenderType[] senderTypes)
        {
            _senderTypes = senderTypes;
        }

        /// <summary>
        /// 发送前校验
        /// </summary>
        /// <returns></returns>
        public BeforeSendValidateState BeforeSendValidate()
        {
            if (_senderTypes == null || !_senderTypes.Any())
            {
                return BeforeSendValidateState.None;
            }

            foreach (var messageSenderType in _senderTypes)
            {
                var sender = MessageSenderFactory.GetMessageSender(messageSenderType);
                var result = sender.BeforeSendValidate();

                if (result != BeforeSendValidateState.Ok)
                {
                    return result;
                }
            }

            return BeforeSendValidateState.Ok;
        }

        public void Send()
        {
            foreach (var messageSenderType in _senderTypes)
            {
                var sender = MessageSenderFactory.GetMessageSender(messageSenderType);
                sender.Send();
            }
        }
    }
}
